#include "peer.h"

NodePeer::NodePeer(Node *parent, NodePeerParam params) :
    Node(parent)
{
    const_peer_id = params.peer_id;
    NodeSoftmemParam node_param_894566352;
    node_param_894566352.softmem_size = params.softmem_size;
    node_param_894566352.num_of_main_memory = params.num_of_main_memory;
    node_param_894566352.num_of_alu = params.num_of_alu;
    node_param_894566352.multiplexer = params.multiplexer;
    node_softmem = new NodeSoftmem(this, node_param_894566352);
    NodeERunnerParam node_param_894565344;
    node_param_894565344.num_of_alu = params.num_of_alu;
    node_param_894565344.peer_id = params.peer_id;
    node_param_894565344.total_peers = params.total_peers;
    node_param_894565344.base_ptrs = params.base_ptrs;
    node_erunner = new NodeERunner(this, node_param_894565344);
    NodeMRunnerParam node_param_894564336;
    List<int> tmp_894564280 = List<int>();
    tmp_894564280.append(params.peer_id);
    node_param_894564336.peer_ids = tmp_894564280;
    node_mrunner = new NodeMRunner(this, node_param_894564336);
    List<NodeAlu *> tmp_894496608;
    for(int temp_885076768=0; temp_885076768<params.num_of_alu; temp_885076768+=1)
    {
        tmp_894496608.append(new NodeAlu(this));
    }
    node_alus = tmp_894496608;
    NodeAndGateParam node_param_894496160;
    node_param_894496160.num_of_ports = (params.num_of_alu)+(1);
    node_halt_detect = new NodeAndGate(this, node_param_894496160);
    port_nano_op_receiver = node_erunner->port_nano_op_receiver;
    port_memory_receivers = node_softmem->port_memory_receivers;
    port_memory_senders = node_softmem->port_memory_senders;
    for(int ii=0; ii<params.num_of_alu; ii+=1)
    {
        ChannelAlu::connect(node_softmem->port_alu_senders[ii], node_alus[ii]->port_receiver);
    }
    for(int ii=0; ii<params.num_of_alu; ii+=1)
    {
        ChannelERunnerSoftmem::connect(node_erunner->port_softmem_senders[ii], node_softmem->port_erunner_receivers[ii]);
    }
    ChannelLoadFabric::connect(node_mrunner->port_message_out, node_softmem->port_mrunner_receiver);
    ChannelLoadFabric::connect(node_softmem->port_mrunner_sender, node_mrunner->port_message_in);
    for(int ii=0; ii<params.num_of_alu; ii+=1)
    {
        node_softmem->port_halt_alu[ii]->connect(node_halt_detect->port_and_ins[ii]);
    }
    node_mrunner->port_halt->connect(node_halt_detect->port_and_ins[-1]);
    port_chain_out = node_mrunner->port_chain_out;
    port_chain_in = node_mrunner->port_chain_in;
    port_halt = node_halt_detect->port_and_out;
}

NodeTaskList NodePeer::tasks() const
{
    NodeTaskList list;
    return list;
}